package com.gavindon.android.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.gavindon.android.base.BaseViewModel
import com.gavindon.android.entity.HomeBannerResp
import com.gavindon.android.entity.RecommendLstResp
import com.gavindon.android.entity.ScanCodeResp
import com.gavindon.android.entity.UserInfoResp
import com.gavindon.android.network.DataResult
import com.gavindon.android.repo.HomeRepo
import kotlinx.coroutines.launch

/**
 * description:
 * Created by liNan on  2022-2-5 13:53
 */
class HomeViewModel : BaseViewModel() {

    private val homeRepo = HomeRepo()
//    val user: LiveData<User> = liveData {
//        val data = database.loadUser() // loadUser is a suspend function.
//        emit(data)
//    }

    private val recommendLiveData: SingleLiveEvent<DataResult<List<RecommendLstResp>>> =
        SingleLiveEvent()

    private val newsLiveData: SingleLiveEvent<DataResult<List<RecommendLstResp>>> =
        SingleLiveEvent()

    private val bannerLiveData: SingleLiveEvent<DataResult<List<HomeBannerResp>>> =
        SingleLiveEvent()

    private val scanCodeLiveData: SingleLiveEvent<DataResult<ScanCodeResp>> =
        SingleLiveEvent()

    private val loginLiveData: SingleLiveEvent<DataResult<ScanCodeResp>> =
        SingleLiveEvent()

    private val loginUserInfoLiveData: SingleLiveEvent<DataResult<UserInfoResp>> =
        SingleLiveEvent()
    val getUserInfoLiveData = loginUserInfoLiveData

    fun getRecommendLst(): SingleLiveEvent<DataResult<List<RecommendLstResp>>> {
        viewModelScope.launch {
            val result = homeRepo.recommendList()
            recommendLiveData.postValue(result)
        }
        return recommendLiveData
    }

    /*获取最新课程*/
    fun getNewsLst(): SingleLiveEvent<DataResult<List<RecommendLstResp>>> {
        viewModelScope.launch {
            val result = homeRepo.newestList()
            newsLiveData.postValue(result)
        }
        return newsLiveData
    }


    fun getSliderBanner(): SingleLiveEvent<DataResult<List<HomeBannerResp>>> {
        viewModelScope.launch {
            val result = homeRepo.sliderBanner()
            bannerLiveData.postValue(result)
        }
        return bannerLiveData
    }

    fun getScanCode(): SingleLiveEvent<DataResult<ScanCodeResp>> {
        viewModelScope.launch {
            val result = homeRepo.scanCode()
            scanCodeLiveData.postValue(result)
        }
        return scanCodeLiveData
    }

    fun getUserLogin(content: String): SingleLiveEvent<DataResult<ScanCodeResp>> {
        viewModelScope.launch {
            val result = homeRepo.userLogin(content)
            loginLiveData.postValue(result)
        }
        return loginLiveData
    }

    fun getUserInfo(): SingleLiveEvent<DataResult<UserInfoResp>> {
        viewModelScope.launch {
            val result = homeRepo.userLoginInfo()
            loginUserInfoLiveData.postValue(result)
        }
        return loginUserInfoLiveData
    }
}